import 'dart:convert';

import 'package:mfkb/config/Config.dart';
import 'package:mfkb/util/HttpUtil.dart';

class FunService{
  Future<dynamic> init() async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/funInit") ;
    } catch (e) {
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }


  Future<dynamic> score({year:""}) async{
    dynamic data ;
    String suffix = "" ;
    if(year != ""){
      suffix = "?year="+year ;
    }
    try {
      if(Config().getSpf().getInt("scoreFlushTime"+year) == null || DateTime.now().millisecond - Config().getSpf().getInt("scoreFlushTime"+year) > 1000*60*60*2){
        data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/score"+suffix) ;
        if(data["code"] == 200){
          Config().getSpf().setString("score"+year, jsonEncode(data["data"])) ;
          Config().getSpf().setInt("scoreFlushTime"+year, DateTime.now().millisecond) ;
        }
      }else{
        data = {"code":200 , "msg":"成功" , "data":jsonDecode(Config().getSpf().getString("score"+year))} ;
      }
    } catch (e) {
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> eArea() async{
    dynamic data ;
    try {
      if(Config().getSpf().getInt("e_areaFlushTime") == null || DateTime.now().millisecond - Config().getSpf().getInt("e_areaFlushTime") > 1000*60*60*24*10){
        data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/e_area") ;
        if(data["code"] == 200){
          Config().getSpf().setString("e_area", jsonEncode(data["data"])) ;
          Config().getSpf().setInt("e_areaFlushTime", DateTime.now().millisecond) ;
        }
      }else{
        data = {"code":200 , "msg":"成功" , "data":jsonDecode(Config().getSpf().getString("e_area"))} ;
      }
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> electricty({area:"" , build:"" , room:""}) async{
    dynamic data ;
    try {
      if(Config().getSpf().getInt("electrictyFlushTime") == null || DateTime.now().millisecond - Config().getSpf().getInt("electrictyFlushTime") > 1000*60*5){
        data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/electricty?area="+area+"&build="+build+"&room="+room) ;
        if(data["code"] == 200){
          Config().getSpf().setString("electricty", jsonEncode(data["data"])) ;
          Config().getSpf().setInt("electrictyFlushTime", DateTime.now().millisecond) ;
        }
      }else{
        data = {"code":200 , "msg":"成功" , "data":jsonDecode(Config().getSpf().getString("electricty"))} ;
      }
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> exam() async{
    dynamic data ;
    try {
      if(Config().getSpf().getInt("examFlushTime") == null || DateTime.now().millisecond - Config().getSpf().getInt("examFlushTime") > 1000*60*60*12){
        data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/exam") ;
        if(data["code"] == 200){
          Config().getSpf().setString("exam", jsonEncode(data["data"])) ;
          Config().getSpf().setInt("examFlushTime", DateTime.now().millisecond) ;
        }
      }else{
        data = {"code":200 , "msg":"成功" , "data":jsonDecode(Config().getSpf().getString("exam"))} ;
      }
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> relations() async{
    dynamic data ;
    try {
      if(Config().getSpf().getInt("relationsFlushTime") == null || DateTime.now().millisecond - Config().getSpf().getInt("relationsFlushTime") > 1000*60*60*24*5){
        data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/relations") ;
        if(data["code"] == 200){
          Config().getSpf().setString("relations", jsonEncode(data["data"])) ;
          Config().getSpf().setInt("relationsFlushTime", DateTime.now().millisecond) ;
        }
      }else{
        data = {"code":200 , "msg":"成功" , "data":jsonDecode(Config().getSpf().getString("relations"))} ;
      }
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> kb() async{
    dynamic data ;
    try {
      if(Config().getSpf().getInt("kbFlushTime") == null || DateTime.now().millisecond - Config().getSpf().getInt("kbFlushTime") > 1000*60*60*24*5){
        data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/kb") ;
        if(data["code"] == 200){
          Config().getSpf().setString("kb", jsonEncode(data["data"])) ;
          Config().getSpf().setInt("kbFlushTime", DateTime.now().millisecond) ;
        }
      }else{
        data = {"code":200 , "msg":"成功" , "data":jsonDecode(Config().getSpf().getString("kb"))} ;
      }
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> phone() async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/phone") ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> borad() async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/borad") ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> driver() async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/driver") ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> news(int page) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/news?page="+page.toString()) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> checkUpdate(String file) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/checkUpdate?file="+file) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> checkFun() async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/checkFun") ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> checkNews(String file) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/checkNews?file="+file) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> payPhone(String phone , String money) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/payPhone?money="+money+"&phone="+phone) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> payDriver(String phone , String type) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/payDriver?type="+type+"&phone="+phone) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }

  Future<dynamic> payBorad(String phone , String room) async{
    dynamic data ;
    try {
      data = await HttpUtil().get(Config().getHost()+"/api/admin/v1/payBorad?room="+room+"&phone="+phone) ;
    } catch (e) { 
      data = {"code":500 , "msg":"网络错误" , "data":""} ;
    }
    return data ;
  }
}